package com.dong.es.entity.searchparam.order;

import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import com.dong.es.entity.EsBaseDTO;
import org.apache.commons.lang3.StringUtils;

/**
 * @description: Es 字段排序参数
 * @author: 陈瑞东
 * @create: 2022-06-23 10:54
 */
public class EsFieldSortParam extends EsSortParam{

    private static final long serialVersionUID = 5125725406436611095L;

    // 字段名
    private String field;
    // 排序方式
    private SortOrder sortOrder;

    public EsFieldSortParam(String field, SortOrder sortOrder) {
        this.field = field;
        this.sortOrder = sortOrder;
    }

    @Override
    protected boolean valid(EsBaseDTO esBaseDTO) {
        return StringUtils.isNotBlank(field) && sortOrder != null;
    }

    @Override
    public void loadTo(SearchRequest.Builder target) {
        target.sort(sort -> sort.field(field -> field.field(this.field).order(this.sortOrder)));
    }
}
